package InRecord;

import java.sql.Connection;
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;
import java.util.Date;  
  
public class UserDao {  
	Connection con =null;  
    String driver ="com.mysql.jdbc.Driver";  
    String url ="jdbc:mysql://192.168.1.3:3306/speech";
    String user ="root";  
    String password ="shanli@123";
	
    public Connection InitDb(){
		
        try {
			Class.forName(driver);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
			return null;
		}  
        try {
			con = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}  
		return con;
	}

    public int findUsername(String username){
        int psw = -1;  
        Connection con =null;  
        PreparedStatement pstmt =null;  
        ResultSet rs = null;  
        try {  
            con = InitDb();
            
            String sql = "select * from tb_User where User_Account=?";  
            pstmt = con.prepareStatement(sql);  
            pstmt.setString(1, username);  
            rs = pstmt.executeQuery();  
            if(rs==null){  
                return -1;  
            }  
            if(rs.next()){  
                psw=rs.getInt("User_ID");  
            }else{  
                psw=0;  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }finally {  
            try {  
                if(pstmt!=null)pstmt.close();  
                if(con!=null)con.close();  
                }   
            catch (SQLException e) {          
                                    }  
        }  
        return psw;  
    }  
    public void addUser(String username,String psw){  
        Connection con =null;  
        PreparedStatement pstmt =null;  
        try {  
            con = InitDb();
            String sql = "INSERT INTO tb_User(User_Account,User_Password) VALUES(?,?)";  
            pstmt = con.prepareStatement(sql);  
            pstmt.setString(1, username);  
            pstmt.setString(2, psw);
            
            pstmt.executeUpdate();  
            
        } catch (SQLException e) {  
            e.printStackTrace();  
        }finally {  
            try {  
                if(pstmt!=null)pstmt.close();  
                if(con!=null)con.close();  
                }   
            catch (SQLException e) {          
                                    }  
        }  
    }  
    //��������ʹ��  
    //public static void main(String[] args) {  
        //String psw =new UserDao().findUsername("345");  
        //System.out.println(psw);  
        //UserDao u = new UserDao();  
        //u.addUser("345", "345");  
    //}  
      
}